package vip.bblog.cunadmin.modules.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import vip.bblog.cunadmin.annotation.group.Default;
import vip.bblog.cunadmin.annotation.group.Update;
import vip.bblog.cunadmin.aopConfig.Log;
import vip.bblog.cunadmin.common.entity.BaseResult;
import vip.bblog.cunadmin.modules.system.dto.ProjectParams;
import vip.bblog.cunadmin.modules.system.entity.Card;
import vip.bblog.cunadmin.modules.system.entity.Publisher;
import vip.bblog.cunadmin.modules.system.service.CardService;
import vip.bblog.cunadmin.modules.system.service.PublisherService;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


/**
 * <p>发行商</p>
 * @author yy
 */
@Api(tags = "发行商")
@ApiSort(1)
@RestController
@RequestMapping("/publisher")
public class PublisherController {

    @Autowired
    private PublisherService publisherService;
    @Autowired
    private CardService cardService;

    @Log(content = "发行商管理",handleType = "1")
    @ApiOperation(value = "添加发行商")
    @PreAuthorize("hasAnyAuthority('sys:publisher:add')")
    @PostMapping
    public BaseResult<String> add(@Validated(Default.class) @RequestBody Publisher info) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String ctime = formatter.format(new Date());
        info.setCreateTime(ctime);
        publisherService.save(info);
        return BaseResult.success();
    }

    @Log(content = "发行商管理",handleType = "2")
    @ApiOperation(value = "修改")
    @PreAuthorize("hasAnyAuthority('sys:publisher:update')")
    @PutMapping
    public BaseResult<Publisher> edit(@Validated(Update.class) @RequestBody Publisher info) {
        Publisher publisher = publisherService.getById(info.getId());
        if(publisher.getNameCode().equals(info.getNameCode())){
            publisherService.updateById(info);
            return BaseResult.success();
        }else{
            publisherService.updateById(info);
            return BaseResult.success();
        }
    }

    @Log(content = "发行商管理",handleType = "3")
    @ApiOperation(value = "刪除信息")
    @PreAuthorize("hasAnyAuthority('sys:publisher:delete')")
    @DeleteMapping("{id}")
    public BaseResult<Publisher> delete(@PathVariable Integer id) {
        publisherService.removeById(id);
        return BaseResult.success();
    }

    @ApiOperation(value = "分页查询")
    @GetMapping
    public BaseResult listPage(ProjectParams params) {
//        IPage<Publisher> page = publisherService.getByPage(params);
//        return BaseResult.success(page);
        return BaseResult.success(publisherService.listPage(params));
    }

}

